// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com

// ReSharper disable CheckNamespace
// ReSharper disable CommentTypo
// ReSharper disable CompareOfFloatsByEqualityOperator
// ReSharper disable InconsistentNaming

/* Symbol.cs --
 * Ars Magna project, http://arsmagna.ru
 */

#region Using directives

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.Serialization;

#endregion

#nullable enable

namespace AM.Drawing.Charting;

/// <summary>
/// This class handles the drawing of the curve <see cref="Symbol"/> objects.
/// The symbols are the small shapes that appear over each defined point
/// along the curve.
/// </summary>
[Serializable]
public class Symbol
    : ICloneable, ISerializable
{
    #region Fields

    /// <summary>
    /// Private field that stores the size of this
    /// <see cref="Symbol"/> in points (1/72 inch).  Use the public
    /// property <see cref="Size"/> to access this value.
    /// </summary>
    private float _size;

    /// <summary>
    /// Private field that stores the <see cref="SymbolType"/> for this
    /// <see cref="Symbol"/>.  Use the public
    /// property <see cref="Type"/> to access this value.
    /// </summary>
    private SymbolType _type;

    /// <summary>
    /// private field that determines if the symbols are drawn using
    /// Anti-Aliasing capabilities from the <see cref="Graphics" /> class.
    /// Use the public property <see cref="IsAntiAlias" /> to access
    /// this value.
    /// </summary>
    private bool _isAntiAlias;

    /// <summary>
    /// Private field that stores the visibility of this
    /// <see cref="Symbol"/>.  Use the public
    /// property <see cref="IsVisible"/> to access this value.  If this value is
    /// false, the symbols will not be shown (but the <see cref="Line"/> may
    /// still be shown).
    /// </summary>
    private bool _isVisible;

    /// <summary>
    /// Private field that stores the <see cref="Charting.Fill"/> data for this
    /// <see cref="Symbol"/>.  Use the public property <see cref="Fill"/> to
    /// access this value.
    /// </summary>
    private Fill _fill;

    /// <summary>
    /// Private field that stores the <see cref="Charting.Border"/> data for this
    /// <see cref="Symbol"/>.  Use the public property <see cref="Border"/> to
    /// access this value.
    /// </summary>
    private Border _border;

    /// <summary>
    /// Private field that stores the user defined <see cref="GraphicsPath"/> data for this
    /// <see cref="Symbol"/>.  Use the public property <see cref="UserSymbol"/> to
    /// access this value.
    /// </summary>
    private GraphicsPath? _userSymbol;

    #endregion

    #region Defaults

    /// <summary>
    /// A simple struct that defines the
    /// default property values for the <see cref="Symbol"/> class.
    /// </summary>
    public struct Default
    {
        // Default Symbol properties
        /// <summary>
        /// The default size for curve symbols (<see cref="Symbol.Size"/> property),
        /// in units of points.
        /// </summary>
        public static float Size = 7;

        /// <summary>
        /// The default pen width to be used for drawing curve symbols
        /// (<see cref="LineBase.Width"/> property).  Units are points.
        /// </summary>
        public static float PenWidth = 1.0F;

        /// <summary>
        /// The default color for filling in this <see cref="Symbol"/>
        /// (<see cref="Fill.Color"/> property).
        /// </summary>
        public static Color FillColor = Color.Red;

        /// <summary>
        /// The default custom brush for filling in this <see cref="Symbol"/>
        /// (<see cref="Fill.Brush"/> property).
        /// </summary>
        public static Brush? FillBrush = null;

        /// <summary>
        /// The default fill mode for the curve (<see cref="Fill.Type"/> property).
        /// </summary>
        public static FillType FillType = FillType.None;

        /// <summary>
        /// The default symbol type for curves (<see cref="Symbol.Type"/> property).
        /// This is defined as a <see cref="SymbolType"/> enumeration.
        /// </summary>
        public static SymbolType Type = SymbolType.Square;

        /// <summary>
        /// The default value for the <see cref="Symbol.IsAntiAlias"/>
        /// property.
        /// </summary>
        public static bool IsAntiAlias = false;

        /// <summary>
        /// The default display mode for symbols (<see cref="Symbol.IsVisible"/> property).
        /// true to display symbols, false to hide them.
        /// </summary>
        public static bool IsVisible = true;

        /// <summary>
        /// The default for drawing frames around symbols (<see cref="LineBase.IsVisible"/> property).
        /// true to display symbol frames, false to hide them.
        /// </summary>
        public static bool IsBorderVisible = true;

        /// <summary>
        /// The default color for drawing symbols (<see cref="LineBase.Color"/> property).
        /// </summary>
        public static Color BorderColor = Color.Red;
    }

    #endregion

    #region Properties

    /// <summary>
    /// Gets or sets the size of the <see cref="Symbol"/>
    /// </summary>
    /// <value>Size in points (1/72 inch)</value>
    /// <seealso cref="Default.Size"/>
    public float Size
    {
        get => _size;
        set => _size = value;
    }

    /// <summary>
    /// Gets or sets the type (shape) of the <see cref="Symbol"/>
    /// </summary>
    /// <value>A <see cref="SymbolType"/> enum value indicating the shape</value>
    /// <seealso cref="Default.Type"/>
    public SymbolType Type
    {
        get => _type;
        set => _type = value;
    }

    /// <summary>
    /// Gets or sets a value that determines if the symbols are drawn using
    /// Anti-Aliasing capabilities from the <see cref="Graphics" /> class.
    /// </summary>
    /// <remarks>
    /// If this value is set to true, then the <see cref="Graphics.SmoothingMode" />
    /// property will be set to <see cref="SmoothingMode.HighQuality" /> only while
    /// this <see cref="Symbol" /> is drawn.  A value of false will leave the value of
    /// <see cref="Graphics.SmoothingMode" /> unchanged.
    /// </remarks>
    public bool IsAntiAlias
    {
        get => _isAntiAlias;
        set => _isAntiAlias = value;
    }

    /// <summary>
    /// Gets or sets a property that shows or hides the <see cref="Symbol"/>.
    /// </summary>
    /// <value>true to show the symbol, false to hide it</value>
    /// <seealso cref="Default.IsVisible"/>
    public bool IsVisible
    {
        get => _isVisible;
        set => _isVisible = value;
    }

    /// <summary>
    /// Gets or sets the <see cref="Charting.Fill"/> data for this
    /// <see cref="Symbol"/>.
    /// </summary>
    public Fill Fill
    {
        get => _fill;
        set => _fill = value;
    }

    /// <summary>
    /// Gets or sets the <see cref="Charting.Border"/> data for this
    /// <see cref="Symbol"/>, which controls the border outline of the symbol.
    /// </summary>
    public Border Border
    {
        get => _border;
        set => _border = value;
    }

    /// <summary>
    /// Gets or sets the <see cref="GraphicsPath"/> data for this
    /// <see cref="Symbol"/>, describing the user-defined symbol type.
    /// </summary>
    /// <remarks>
    /// This value only applies if <see cref="Symbol.Type">Symbol.Type</see>
    /// is <see cref="SymbolType.UserDefined">SymbolType.UserDefined</see>
    /// </remarks>
    public GraphicsPath? UserSymbol
    {
        get => _userSymbol;
        set => _userSymbol = value;
    }

    #endregion

    #region Constructors

    /// <summary>
    /// Default constructor that sets all <see cref="Symbol"/> properties to default
    /// values as defined in the <see cref="Default"/> class.
    /// </summary>
    public Symbol()
        : this (SymbolType.Default, Color.Empty)
    {
        // пустое тело конструктора
    }

    /// <summary>
    /// Default constructor that sets the <see cref="SymbolType"/> and
    /// <see cref="Color"/> as specified, and the remaining
    /// <see cref="Symbol"/> properties to default
    /// values as defined in the <see cref="Default"/> class.
    /// </summary>
    /// <param name="type">A <see cref="SymbolType"/> enum value
    /// indicating the shape of the symbol</param>
    /// <param name="color">A <see cref="Color"/> value indicating
    /// the color of the symbol
    /// </param>
    public Symbol
        (
            SymbolType type,
            Color color
        )
    {
        _size = Default.Size;
        _type = type;
        _isAntiAlias = Default.IsAntiAlias;
        _isVisible = Default.IsVisible;
        _border = new Border (Default.IsBorderVisible, color, Default.PenWidth);
        _fill = new Fill (color, Default.FillBrush, Default.FillType);
        _userSymbol = null;
    }

    /// <summary>
    /// The Copy Constructor
    /// </summary>
    /// <param name="rhs">The Symbol object from which to copy</param>
    public Symbol (Symbol rhs)
    {
        _size = rhs.Size;
        _type = rhs.Type;
        _isAntiAlias = rhs._isAntiAlias;
        _isVisible = rhs.IsVisible;
        _fill = rhs.Fill.Clone();
        _border = rhs.Border.Clone();

        if (rhs.UserSymbol != null)
        {
            _userSymbol = rhs.UserSymbol.Clone() as GraphicsPath;
        }
        else
        {
            _userSymbol = null;
        }
    }

    /// <inheritdoc cref="ICloneable.Clone"/>
    object ICloneable.Clone()
    {
        return Clone();
    }

    /// <summary>
    /// Typesafe, deep-copy clone method.
    /// </summary>
    /// <returns>A new, independent copy of this class</returns>
    public Symbol Clone()
    {
        return new Symbol (this);
    }

    #endregion

    #region Serialization

    /// <summary>
    /// Current schema value that defines the version of the serialized file
    /// </summary>
    public const int schema = 11;

    /// <summary>
    /// Constructor for deserializing objects
    /// </summary>
    /// <param name="info">A <see cref="SerializationInfo"/> instance that defines the serialized data
    /// </param>
    /// <param name="context">A <see cref="StreamingContext"/> instance that contains the serialized data
    /// </param>
    protected Symbol
        (
            SerializationInfo info,
            StreamingContext context
        )
    {
        // The schema value is just a file version parameter.  You can use it to make future versions
        // backwards compatible as new member variables are added to classes
        var sch = info.GetInt32 ("schema");

        _size = info.GetSingle ("size");
        _type = (SymbolType) info.GetValue ("type", typeof (SymbolType))!;
        _isAntiAlias = info.GetBoolean ("isAntiAlias");
        _isVisible = info.GetBoolean ("isVisible");
        _fill = (Fill) info.GetValue ("fill", typeof (Fill))!;
        _border = (Border) info.GetValue ("border", typeof (Border))!;

        if (sch >= 11)
        {
            _userSymbol = (GraphicsPath?) info.GetValue ("userSymbol", typeof (GraphicsPath));
        }
        else
        {
            _userSymbol = null;
        }
    }

    /// <inheritdoc cref="ISerializable.GetObjectData"/>
    public virtual void GetObjectData
        (
            SerializationInfo info,
            StreamingContext context
        )
    {
        info.AddValue ("schema", schema);
        info.AddValue ("size", _size);
        info.AddValue ("type", _type);
        info.AddValue ("isAntiAlias", _isAntiAlias);
        info.AddValue ("isVisible", _isVisible);
        info.AddValue ("fill", _fill);
        info.AddValue ("border", _border);
        info.AddValue ("userSymbol", _userSymbol);
    }

    #endregion

    #region Rendering Methods

    /// <summary>
    /// Draw the <see cref="Symbol"/> to the specified <see cref="Graphics"/> device
    /// at the specified location.  This routine draws a single symbol.
    /// </summary>
    /// <param name="graphics">
    /// A graphic device object to be drawn into.  This is normally e.Graphics from the
    /// PaintEventArgs argument to the Paint() method.
    /// </param>
    /// <param name="x">The x position of the center of the symbol in
    /// pixel units</param>
    /// <param name="y">The y position of the center of the symbol in
    /// pixel units</param>
    /// <param name="path">A <see cref="GraphicsPath"/> previously constructed by
    /// <see cref="MakePath"/> for this symbol</param>
    /// <param name="pen">A <see cref="Pen"/> class representing the standard pen for this symbol</param>
    /// <param name="brush">A <see cref="Brush"/> class representing a default solid brush for this symbol
    /// If this symbol uses a <see cref="LinearGradientBrush"/>, it will be created on the fly for
    /// each point, since it has to be scaled to the individual point coordinates.</param>
    private void DrawSymbol
        (
            Graphics graphics,
            int x,
            int y,
            GraphicsPath path,
            Pen pen,
            Brush brush
        )
    {
        // Only draw if the symbol is visible
        if (_isVisible &&
            Type != SymbolType.None &&
            x is < 100000 and > -100000 &&
            y is < 100000 and > -100000)
        {
            var saveMatrix = graphics.Transform;
            graphics.TranslateTransform (x, y);

            // Fill or draw the symbol as required
            if (_fill.IsVisible)
            {
                graphics.FillPath (brush, path);
            }

            //FillPoint( g, x, y, scaleFactor, pen, brush );

            if (_border.IsVisible)
            {
                graphics.DrawPath (pen, path);
            }

            //DrawPoint( g, x, y, scaleFactor, pen );

            graphics.Transform = saveMatrix;
        }
    }

    /// <summary>
    /// Draw the <see cref="Symbol"/> to the specified <see cref="Graphics"/> device
    /// at the specified location.  This routine draws a single symbol.
    /// </summary>
    /// <param name="graphics">
    /// A graphic device object to be drawn into.  This is normally e.Graphics from the
    /// PaintEventArgs argument to the Paint() method.
    /// </param>
    /// <param name="pane">
    /// A reference to the <see cref="GraphPane"/> object that is the parent or
    /// owner of this object.
    /// </param>
    /// <param name="x">The x position of the center of the symbol in
    /// pixel units</param>
    /// <param name="y">The y position of the center of the symbol in
    /// pixel units</param>
    /// <param name="scaleFactor">
    /// The scaling factor for the features of the graph based on the <see cref="PaneBase.BaseDimension"/>.  This
    /// scaling factor is calculated by the <see cref="PaneBase.CalcScaleFactor"/> method.  The scale factor
    /// represents a linear multiple to be applied to font sizes, symbol sizes, etc.
    /// </param>
    /// <param name="dataValue">The data value to be used for a value-based
    /// color gradient.  This is only applicable for <see cref="FillType.GradientByX"/>,
    /// <see cref="FillType.GradientByY"/> or <see cref="FillType.GradientByZ"/>.</param>
    /// <param name="isSelected">Indicates that the <see cref="Symbol" /> should be drawn
    /// with attributes from the <see cref="Selection" /> class.
    /// </param>
    public void DrawSymbol
        (
            Graphics graphics,
            GraphPane pane,
            int x,
            int y,
            float scaleFactor,
            bool isSelected,
            PointPair? dataValue
        )
    {
        // Only draw if the symbol is visible
        if (_isVisible &&
            Type != SymbolType.None &&
            x is < 100000 and > -100000 &&
            y is < 100000 and > -100000)
        {
            var sModeSave = graphics.SmoothingMode;
            if (_isAntiAlias)
            {
                graphics.SmoothingMode = SmoothingMode.HighQuality;
            }

            using (var pen = _border.GetPen (pane, scaleFactor, dataValue))
            using (var path = MakePath (graphics, scaleFactor))
            using (var brush = Fill.MakeBrush (path.GetBounds(), dataValue))
            {
                DrawSymbol (graphics, x, y, path, pen, brush);
            }

            graphics.SmoothingMode = sModeSave;
        }
    }

    /// <summary>
    /// Create a <see cref="GraphicsPath"/> struct for the current symbol based on the
    /// specified scaleFactor and assuming the symbol will be centered at position 0,0.
    /// </summary>
    /// <param name="graphics">
    /// A graphic device object to be drawn into.  This is normally e.Graphics from the
    /// PaintEventArgs argument to the Paint() method.
    /// </param>
    /// <param name="scaleFactor">
    /// The scaling factor for the features of the graph based on the <see cref="PaneBase.BaseDimension"/>.  This
    /// scaling factor is calculated by the <see cref="PaneBase.CalcScaleFactor"/> method.  The scale factor
    /// represents a linear multiple to be applied to font sizes, symbol sizes, etc.</param>
    /// <returns>Returns the <see cref="GraphicsPath"/> for the current symbol</returns>
    public GraphicsPath MakePath
        (
            Graphics graphics,
            float scaleFactor
        )
    {
        var scaledSize = _size * scaleFactor;
        float hsize = scaledSize / 2,
            hsize1 = hsize + 1;

        var path = new GraphicsPath();

        switch (_type == SymbolType.Default || (_type == SymbolType.UserDefined && _userSymbol == null)
                    ? Default.Type
                    : _type)
        {
            case SymbolType.Square:
                path.AddLine (-hsize, -hsize, hsize, -hsize);
                path.AddLine (hsize, -hsize, hsize, hsize);
                path.AddLine (hsize, hsize, -hsize, hsize);
                path.AddLine (-hsize, hsize, -hsize, -hsize);
                break;

            case SymbolType.Diamond:
                path.AddLine (0, -hsize, hsize, 0);
                path.AddLine (hsize, 0, 0, hsize);
                path.AddLine (0, hsize, -hsize, 0);
                path.AddLine (-hsize, 0, 0, -hsize);
                break;

            case SymbolType.Triangle:
                path.AddLine (0, -hsize, hsize, hsize);
                path.AddLine (hsize, hsize, -hsize, hsize);
                path.AddLine (-hsize, hsize, 0, -hsize);
                break;

            case SymbolType.Circle:
                path.AddEllipse (-hsize, -hsize, scaledSize, scaledSize);
                break;

            case SymbolType.XCross:
                path.AddLine (-hsize, -hsize, hsize1, hsize1);
                path.StartFigure();
                path.AddLine (hsize, -hsize, -hsize1, hsize1);
                break;

            case SymbolType.Plus:
                path.AddLine (0, -hsize, 0, hsize1);
                path.StartFigure();
                path.AddLine (-hsize, 0, hsize1, 0);
                break;

            case SymbolType.Star:
                path.AddLine (0, -hsize, 0, hsize1);
                path.StartFigure();
                path.AddLine (-hsize, 0, hsize1, 0);
                path.StartFigure();
                path.AddLine (-hsize, -hsize, hsize1, hsize1);
                path.StartFigure();
                path.AddLine (hsize, -hsize, -hsize1, hsize1);
                break;

            case SymbolType.TriangleDown:
                path.AddLine (0, hsize, hsize, -hsize);
                path.AddLine (hsize, -hsize, -hsize, -hsize);
                path.AddLine (-hsize, -hsize, 0, hsize);
                break;

            case SymbolType.HDash:
                path.AddLine (-hsize, 0, hsize1, 0);
                break;

            case SymbolType.VDash:
                path.AddLine (0, -hsize, 0, hsize1);
                break;

            case SymbolType.UserDefined:
                path = _userSymbol!.Clone() as GraphicsPath;
                var scaleTransform = new Matrix (scaledSize, 0.0f, 0.0f, scaledSize, 0.0f, 0.0f);
                path!.Transform (scaleTransform);
                break;
        }

        return path;
    }

    /// <summary>
    /// Draw this <see cref="CurveItem"/> to the specified <see cref="Graphics"/>
    /// device as a symbol at each defined point.  The routine
    /// only draws the symbols; the lines are draw by the
    /// <see cref="Line.DrawCurve"/> method.  This method
    /// is normally only called by the Draw method of the
    /// <see cref="CurveItem"/> object
    /// </summary>
    /// <param name="graphics">
    /// A graphic device object to be drawn into.  This is normally e.Graphics from the
    /// PaintEventArgs argument to the Paint() method.
    /// </param>
    /// <param name="pane">
    /// A reference to the <see cref="GraphPane"/> object that is the parent or
    /// owner of this object.
    /// </param>
    /// <param name="curve">A <see cref="LineItem"/> representing this
    /// curve.</param>
    /// <param name="scaleFactor">
    /// The scaling factor to be used for rendering objects.  This is calculated and
    /// passed down by the parent <see cref="GraphPane"/> object using the
    /// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust
    /// font sizes, etc. according to the actual size of the graph.
    /// </param>
    /// <param name="isSelected">Indicates that the <see cref="Symbol" /> should be drawn
    /// with attributes from the <see cref="Selection" /> class.
    /// </param>
    public void Draw
        (
            Graphics graphics,
            GraphPane pane,
            LineItem curve,
            float scaleFactor,
            bool isSelected
        )
    {
        var source = this;
        if (isSelected)
        {
            source = Selection.Symbol;
        }

        int tmpX, tmpY;

        var minX = (int)pane.Chart.Rect.Left;
        var maxX = (int)pane.Chart.Rect.Right;
        var minY = (int)pane.Chart.Rect.Top;
        var maxY = (int)pane.Chart.Rect.Bottom;

        // (Dale-a-b) we'll set an element to true when it has been drawn
        var isPixelDrawn = new bool[maxX + 1, maxY + 1];

        double curX, curY;
        var points = curve.Points;

        if (points != null && (_border.IsVisible || _fill.IsVisible))
        {
            var sModeSave = graphics.SmoothingMode;
            if (_isAntiAlias)
            {
                graphics.SmoothingMode = SmoothingMode.HighQuality;
            }

            // For the sake of speed, go ahead and create a solid brush and a pen
            // If it's a gradient fill, it will be created on the fly for each symbol
            //SolidBrush	brush = new SolidBrush( this.fill.Color );

            using (var pen = source._border.GetPen (pane, scaleFactor))
            using (var path = MakePath (graphics, scaleFactor))
            {
                var rect = path.GetBounds();

                using (var brush = source.Fill.MakeBrush (rect))
                {
                    var valueHandler = new ValueHandler (pane, false);
                    var xScale = curve.GetXAxis (pane).Scale;
                    var yScale = curve.GetYAxis (pane).Scale;

                    var xIsLog = xScale!.IsLog;
                    var yIsLog = yScale!.IsLog;
                    var xIsOrdinal = xScale.IsAnyOrdinal;

                    var xMin = xScale.Min;
                    var xMax = xScale.Max;

                    // Loop over each defined point
                    for (var i = 0; i < points.Count; i++)
                    {
                        // Get the user scale values for the current point
                        // use the valueHandler only for stacked types
                        if (pane.LineType == LineType.Stack)
                        {
                            valueHandler.GetValues (curve, i, out curX, out _, out curY);
                        }

                        // otherwise, just access the values directly.  Avoiding the valueHandler for
                        // non-stacked types is an optimization to minimize overhead in case there are
                        // a large number of points.
                        else
                        {
                            curX = points[i].X;
                            if (curve is StickItem)
                            {
                                curY = points[i].Z;
                            }
                            else
                            {
                                curY = points[i].Y;
                            }
                        }

                        // Any value set to double max is invalid and should be skipped
                        // This is used for calculated values that are out of range, divide
                        //   by zero, etc.
                        // Also, any value <= zero on a log scale is invalid

                        if (curX != PointPairBase.Missing &&
                            curY != PointPairBase.Missing &&
                            !double.IsNaN (curX) &&
                            !double.IsNaN (curY) &&
                            !double.IsInfinity (curX) &&
                            !double.IsInfinity (curY) &&
                            (curX > 0 || !xIsLog) &&
                            (!yIsLog || curY > 0.0) &&
                            (xIsOrdinal || (curX >= xMin && curX <= xMax)))
                        {
                            // Transform the user scale values to pixel locations
                            tmpX = (int)xScale.Transform (curve.IsOverrideOrdinal, i, curX);
                            tmpY = (int)yScale.Transform (curve.IsOverrideOrdinal, i, curY);

                            // Maintain an array of "used" pixel locations to avoid duplicate drawing operations
                            if (tmpX >= minX && tmpX <= maxX && tmpY >= minY &&
                                tmpY <= maxY) // guard against the zoom-in case
                            {
                                if (isPixelDrawn[tmpX, tmpY])
                                {
                                    continue;
                                }

                                isPixelDrawn[tmpX, tmpY] = true;
                            }

                            // If the fill type for this symbol is a Gradient by value type,
                            // the make a brush corresponding to the appropriate current value
                            if (_fill.IsGradientValueType || _border.GradientFill.IsGradientValueType)
                            {
                                using (var tBrush = _fill.MakeBrush (rect, points[i]))
                                using (var tPen = _border.GetPen (pane, scaleFactor, points[i]))
                                    DrawSymbol (graphics, tmpX, tmpY, path, tPen, tBrush);
                            }
                            else
                            {
                                // Otherwise, the brush is already defined
                                // Draw the symbol at the specified pixel location
                                DrawSymbol (graphics, tmpX, tmpY, path, pen, brush);
                            }
                        }
                    }
                }
            }

            graphics.SmoothingMode = sModeSave;
        }
    }

    #endregion
}
